home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / sound / bufcallback / bufferplay.c < prev   
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.6 KB  |  284 lines

  1. /*
  2.     File:        BufferPlay.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: RICHARD P. COLLYER    
  7.  
  8.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 8/2/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                                             Fixed Menu bug
  21.                 
  22.  
  23. */
  24. #include     <Fonts.h>
  25. #include    <Quickdraw.h>
  26. #include    <Windows.h>
  27. #include    <desk.h>
  28. #include    <Memory.h>
  29. #include    <Menus.h>
  30. #include    <OSEvents.h>
  31. #include    <Resources.h>
  32. #include    <SegLoad.h>
  33. #include    <Sound.h>
  34. #include    <ToolUtils.h>
  35. #include    <Processes.h>
  36.  
  37. #ifdef applec
  38.     extern _DataInit();
  39. #endif
  40.  
  41. #define    TRUE            0xFF
  42. #define    FALSE            0
  43.  
  44. #define    appleID            128            
  45. #define    appleMenu        0
  46. #define    aboutMeCommand    1
  47.  
  48. #define    fileID            129
  49. #define    startCommand    1
  50. #define printCommand    3
  51. #define    quitCommand     5
  52.  
  53. #define    editID            130
  54.  
  55. int                    yieldTime, err;
  56. MenuHandle            mymenu1, mymenu0;
  57. EventRecord         myEvent;
  58. Boolean                DoneFlag;
  59. WindowPtr            whichWindow;
  60. short                GlobalFlag;
  61.  
  62. pascal void CallBack(SndChannelPtr chan, SndCommand cmd);
  63. void start();
  64. void showAboutMeDialog();
  65. void doCommand(long mResult);
  66. void init();
  67.  
  68. /*______________________________________________________*/
  69. /*                What ever You want                    */
  70. /*______________________________________________________*/
  71. pascal void CallBack(SndChannelPtr chan, SndCommand cmd)
  72. {
  73. #pragma unused (chan)
  74. #pragma unused (cmd)
  75.  
  76.   #ifndef powerc
  77.       long    myA5;
  78.       myA5 = SetA5 (cmd.param2);
  79.   #endif
  80.     
  81.     GlobalFlag = TRUE;
  82.    
  83.    #ifndef powerc
  84.       myA5 = SetA5 (myA5);
  85.    #endif
  86.  
  87.     return;
  88. }
  89.     
  90. /*______________________________________________________*/
  91. /*                What ever You want                    */
  92. /*______________________________________________________*/
  93. void start()
  94. {
  95.     Handle            Sound;
  96.     SndChannelPtr    chan;
  97.     SndCommand        mycmd;
  98.     SndCallBackUPP  myCallBackUPP;
  99.     
  100.     GlobalFlag = FALSE;
  101.     
  102.     Sound = GetResource ('snd ', 100);
  103.     if (ResError() != noErr || Sound == nil)
  104.         Debugger();
  105.         
  106.     /* create a UPP for our sound callback */
  107.     myCallBackUPP = NewSndCallBackProc(CallBack);
  108.     
  109.     chan = nil;
  110.     err = SndNewChannel (&chan, sampledSynth, initNoInterp + initMono, myCallBackUPP);
  111.     if (err != noErr)
  112.         Debugger();
  113.     
  114.     do {
  115.         mycmd.cmd = bufferCmd;
  116.         mycmd.param1 = 0;
  117.         mycmd.param2 = (long) *Sound;
  118.         err = SndDoCommand (chan, &mycmd, FALSE);
  119.         if (err != noErr)
  120.             Debugger();
  121.         
  122.         mycmd.cmd = callBackCmd;
  123.         mycmd.param1 = 0;
  124.         mycmd.param2 = SetCurrentA5();
  125.         err = SndDoCommand (chan, &mycmd, FALSE);
  126.         if (err != noErr)
  127.             Debugger();
  128.         
  129.         do{
  130.             }while (GlobalFlag == FALSE);
  131.  
  132.         GlobalFlag = FALSE;
  133.  
  134.         } while (!Button());
  135.         
  136.     err = SndDisposeChannel (chan,FALSE);
  137.     if (err != noErr)
  138.         Debugger();
  139.         
  140.     DisposeRoutineDescriptor(myCallBackUPP);
  141.  
  142.     return;
  143. }
  144.  
  145. /*______________________________________________________*/
  146. /*                 About Prog Dialog                    */
  147. /*______________________________________________________*/
  148. void showAboutMeDialog()
  149. {
  150.     return;
  151. }
  152.  
  153. /*______________________________________________________*/
  154. /*                 Do Menu Function                     */
  155. /*______________________________________________________*/
  156. void doCommand(mResult)
  157.     long    mResult;
  158. {
  159.     int                     theMenu, theItem;
  160.     Str255                    daName;
  161.     GrafPtr                 savePort;
  162.  
  163.     theItem = LoWord(mResult);
  164.     theMenu = HiWord(mResult);
  165.     
  166.     switch (theMenu) {
  167. /*______________________________________________________*/
  168. /*                    Do Apple Menu                     */
  169. /*______________________________________________________*/
  170.         case appleID:
  171.             if (theItem == aboutMeCommand)
  172.                 showAboutMeDialog();
  173.             else {
  174.                 GetMenuItemText(mymenu0, theItem, daName);
  175.                 GetPort(&savePort);
  176.                 (void) OpenDeskAcc((ConstStr255Param)daName);
  177.                 SetPort(savePort);
  178.             }
  179.             break;
  180. /*______________________________________________________*/
  181. /*                     Do File Menu                     */
  182. /*______________________________________________________*/
  183.         case fileID:
  184.             switch (theItem) {
  185.                 case startCommand:
  186.                     start();
  187.                     break;
  188.                 case printCommand:
  189.                     break;
  190.                 case quitCommand:
  191.                     DoneFlag = TRUE;
  192.                     break;
  193.                 default:
  194.                     break;
  195.                 }
  196.             break;
  197. /*______________________________________________________*/
  198. /*                     Do Edit Menu                     */
  199. /*______________________________________________________*/
  200.         case editID:
  201.             switch (theItem) {
  202.                 default:
  203.                     break;
  204.                 }
  205.             break;
  206.         default:
  207.             break;
  208.         }
  209.     HiliteMenu(0);
  210.     return;
  211. }
  212.  
  213. /*______________________________________________________*/
  214. /*               Initialization traps                   */
  215. /*______________________________________________________*/
  216. void init()
  217. {
  218.     #ifdef applec
  219.         UnloadSeg(_DataInit);
  220.     #endif     
  221.     InitGraf(&qd.thePort);
  222.     InitFonts();
  223.     FlushEvents(everyEvent, 0);
  224.     InitWindows();
  225.     InitMenus();
  226.     TEInit();
  227.     InitDialogs(nil);
  228.     InitCursor();
  229. /*_____________________________________________________*/
  230. /*                    Set menus                         */
  231. /*______________________________________________________*/
  232.     
  233.     SetMenuBar (GetNewMBar (128));
  234.     mymenu0 = GetMenuHandle (128);
  235.     AppendResMenu(mymenu0, 'DRVR');
  236.     mymenu1 = GetMenuHandle(129);
  237.     DrawMenuBar();
  238.  
  239. /*______________________________________________________*/
  240. /*                  Init variables                      */
  241. /*______________________________________________________*/
  242.     DoneFlag = FALSE;
  243.     yieldTime = 0;
  244.     return;
  245. }
  246.  
  247. main()
  248. {
  249. /*______________________________________________________*/
  250. /*                   Main Event loop                    */
  251. /*______________________________________________________*/
  252.     init();
  253.     for ( ;; ) {
  254.         if (DoneFlag) {
  255.             ExitToShell();
  256.             }
  257.         if (WaitNextEvent(everyEvent, &myEvent, yieldTime, nil)) {
  258.             switch (myEvent.what) {
  259.                 case mouseDown:
  260.                     switch (FindWindow(myEvent.where, &whichWindow)) {
  261.                         case inSysWindow:
  262.                             SystemClick(&myEvent, whichWindow);
  263.                             break;
  264.                         case inMenuBar:
  265.                             doCommand(MenuSelect(myEvent.where));
  266.                             break;
  267.                         default:
  268.                             break;
  269.                         }
  270.                     break;
  271.                 case 15:
  272.                     if ((myEvent.message << 31) == 0) { /* Suspend */
  273.                         yieldTime = 30;
  274.                         }
  275.                     else { /* Resume */
  276.                         yieldTime = 0;
  277.                         }
  278.                     break;
  279.                 default:
  280.                     break;
  281.                 }
  282.             }
  283.         }
  284. }